//
//  HomeImageView.m
//  SuperRadio
//
//  Created by Damon on 14/12/17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "HomeImageView.h"
#import "FocusImages.h"
#import "UIImageView+WebCache.h"

@interface HomeImageView ()<UIScrollViewDelegate>

// 定义一个page.用来记录当前显示页
@property (nonatomic ,assign) unsigned int index;
// 记录控件的高
@property (nonatomic, assign) NSInteger height;
// 记录控件的宽
@property (nonatomic, assign) NSInteger width;
// 存储照片数量
@property (nonatomic, assign) unsigned int count;

// 照片数组
@property (nonatomic, retain) NSArray *arr;
// pageControl
@property (nonatomic, retain) UIPageControl *pageControl;

@end

@implementation HomeImageView


- (instancetype)initWithFrame:(CGRect)frame FocusImagesarr:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        // 为成员变量赋值
        self.backgroundColor = [UIColor whiteColor];
        self.width = frame.size.width;
        self.height = frame.size.height;
        self.index = 0;
        self.count = (unsigned int)[arr count];
      // NSLog(@"%d", self.count);
        self.arr = [NSArray arrayWithArray:arr];
        self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        // 创建Myscrollview 的大小, 大小等于照片的数量乘以宽度
        self.scrollView.contentSize = CGSizeMake(self.width * (self.count + 2), 0);
        // Myscrollview可以滑动
        self.scrollView.scrollEnabled = YES;
        // Myscrollview 按页滑动
        self.scrollView.pagingEnabled = YES;
        // 初始化偏移量为第一张图片的位置
        self.scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
        // 隐藏水平滑动条
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        [self addSubview:self.scrollView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        FocusImages *imageStr = [[FocusImages alloc] init];
        imageView.backgroundColor = [UIColor blackColor];
        
        imageStr = [arr objectAtIndex: self.count - 1];
        
        [imageView setImageWithURL:[NSURL URLWithString: [imageStr.pic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [imageStr release];
        [self.scrollView  addSubview:imageView];
        [imageView release];
        
        int i = 1;
        for (FocusImages *focusImage in arr) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.width, 0, self.width, self.height)];
            
            [imageView setImageWithURL:[NSURL URLWithString: [focusImage.pic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
            
            [self.scrollView addSubview:imageView];
            [imageView release];
            i++;
        }
        
        
        UIImageView *imageViewLast = [[UIImageView alloc] initWithFrame:CGRectMake((self.count + 1) * self.width, 0, self.width, frame.size.height)];
        FocusImages *imageStrLast = [[FocusImages alloc] init];
        imageViewLast.backgroundColor = [UIColor blackColor];
        
        imageStrLast = [arr objectAtIndex: 0];
        
        [imageViewLast setImageWithURL:[NSURL URLWithString: [imageStrLast.pic stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [imageStrLast release];
        [self.scrollView  addSubview:imageViewLast];
        [imageViewLast release];
        
        [_scrollView release];
        
       //[self style];
    }
    return self;
}

// 显示风格
- (void)style
{
//    if (self.count <= 15) {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 110, 375, 10)];
        self.pageControl.numberOfPages = 6;
        self.pageControl.pageIndicatorTintColor = [UIColor blueColor];
        self.pageControl.currentPage = 0;
        [self.pageControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
        [self addSubview: self.pageControl];
        [_pageControl release];
        
//    }
    
}

- (void)dealloc
{
    [_scrollView release];
    [_pageControl release];
    [_arr release];
    [super dealloc];
}

// PageControl点击事件
-(void)change:(UIPageControl *)pageControl
{
    
    if (pageControl.currentPage < self.index) {
        // 往回翻
        [self front];
        self.scrollView.contentOffset = CGPointMake(self.width * 2, 0);
        [self.scrollView setContentOffset:CGPointMake(self.width, 0) animated:YES];
    } else if(pageControl.currentPage > self.index) {
        
        
        // 向后翻
        [self behind];
        self.scrollView.contentOffset = CGPointMake(0, 0);
        [self.scrollView setContentOffset:CGPointMake(self.width, 0) animated:YES];
        
    }
    
    
}





- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    if (self.scrollView == scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.index = 0;
            self.scrollView.contentOffset = CGPointMake(self.width * self.count, 0);
            // [self front];
        }else if (self.scrollView.contentOffset.x == self.width * (self.count + 1)) {
            self.scrollView.contentOffset = CGPointMake(self.width, 0);
                      //[self behind];
        }
    }
    self.index = self.scrollView.contentOffset.x / self.width - 1;

    self.pageControl.currentPage = self.index;
    
}


// 向后翻
- (void)behind
{
    
    
}

// 向前翻
- (void)front
{
    
    
    
}




/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
